package cn.shyouchi.wuguanjia.android.utils;

import android.annotation.SuppressLint;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by sll on 2015/3/7.
 */
public class FormatUtil {
    public final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    /**
     * 从URL中提取文件名
     *
     * @param url
     * @return
     */
    public static String getFileNameFromUrl(String url) {
        String filename = url.substring(url.lastIndexOf('/') + 1);
        return filename;
    }

    /**
     * 获取目录文件大小
     */
    public static long getDirSize(File dir) {
        if (dir == null) {
            return 0;
        }
        if (!dir.isDirectory()) {
            return 0;
        }
        long dirSize = 0;
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                dirSize += file.length();
            } else if (file.isDirectory()) {
                dirSize += file.length();
                dirSize += getDirSize(file); // 递归调用继续统计
            }
        }
        return dirSize;
    }

    /**
     * 转换文件大小
     *
     * @return B/KB/MB/GB
     */
    public static String formatFileSize(long fileS) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "G";
        }
        return fileSizeString;
    }


    public static String timeHm() {

        return "";
    }


    @SuppressLint("SimpleDateFormat")
    public static String convertLongDate(String longDate) {
        Date date;
        try {
            date = DATE_FORMAT.parse(longDate);
        } catch (ParseException e) {
            e.printStackTrace();
            date = new Date();
        }
        return new SimpleDateFormat("MM-dd HH:mm").format(date);
    }


}
